package cn.shangqizhenshuai.core;

import cn.shangqizhenshuai.entity.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 创建Xml文件
 */
public class MapperXmlExecute extends ExecuteCore {

    @Override
    public HashMap<String, List> execute(HashMap<String, List> context) throws IOException, TemplateException {


        //从上下文中获取表信息
        List<JavaEntity> entityinfo = context.get("entityinfo");
        List<MapperEntity> mapperinfo = context.get("mapperinfo");

        //配置模板文件对象
        Configuration conf=new Configuration();
        //导入模板目录
        File file = new File("src/main/resources/");
        conf.setDirectoryForTemplateLoading(file);
        Template temp = conf.getTemplate(this.getFTLname());
        //开始文件输出
        String packagename = this.getPackagename();
        String[] split = packagename.split("\\.");
        String outpath = "src/main/java/"+this.getOutFilePath();
        for (String s : split) {
            outpath +="/"+s;
        }
        //
        ArrayList<XmlEntity> xmlEntities = new ArrayList<XmlEntity>();
        //创建文件夹
        File file1 = new File(outpath);
        file1.mkdirs();
        int i = 0;
        for (JavaEntity javaEntity : entityinfo) {
            ArrayList<String> tabkeys = new ArrayList<String>();
            for (ColumnEntity columnEntity : javaEntity.tableEntity.getColumnEntitys()) {
                if(columnEntity.isCOLUMN_KEY()){
                    tabkeys.add(columnEntity.getCOLUMN_NAME());
                }
            }

            XmlEntity xmlEntity = new XmlEntity();
            xmlEntity.setTabkeys(tabkeys);
            xmlEntity.setImPackage(javaEntity.getImPackage());
            xmlEntity.setMapperpackagename(mapperinfo.get(i).getImPackage());
            xmlEntity.setEntitypackagename(javaEntity.getImPackage());
            xmlEntity.setColumnnameandtypemap(javaEntity.getColumnnameandtypemap());
            xmlEntity.setJavaname(javaEntity.getJavaname());
            xmlEntity.setImpotrs(javaEntity.getImpotrs());
            xmlEntity.setIsdetailed(mapperinfo.get(i).isIsdetailed());
            xmlEntity.setJavaname_tabname_map(javaEntity.getJavaname_tabname_map());
            xmlEntity.setTableEntity(javaEntity.getTableEntity());
            xmlEntity.setTabname_javaname_map(javaEntity.getTabname_javaname_map());
            xmlEntities.add(xmlEntity);
            Writer out=new FileWriter(new File(outpath+"/"+javaEntity.getJavaname()+"Mapper.xml"),false);
            temp.process(xmlEntity, out);
            out.flush();
            out.close();
        }
        ArrayList<JavaEntity> javaEntities = new ArrayList<JavaEntity>();
        for (JavaEntity javaEntity : entityinfo) {
            javaEntity.setImPackage(packagename);
            javaEntities.add(javaEntity);
        }
        context.put("mapperxmlinfo",xmlEntities);
        return context;
    }
}
